package de.koller.worlders.render;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.WSea;
import de.koller.worlders.render.util.TextureRep;
import de.koller.worlders.render.visual.DisplayRenderObject;
import de.koller.worlders.render.visual.RotationDRO;
import de.koller.worlders.render.visual.ScrollerDRO;
import java.util.HashSet;
import java.util.Set;

public class VisualMap extends WSea {
	
	@Child public static final DisplayRenderObject invisible = new DisplayRenderObject( 0x00000000 );
	@Child public static final DisplayRenderObject emptyTile = new DisplayRenderObject( 0xff000000 );
	@Child public static final DisplayRenderObject backTile = new DisplayRenderObject( 0xff404040 );
	@Child public static final DisplayRenderObject gridO		= make(    0,  128 );
	@Child public static final DisplayRenderObject gridU		= make(   32,  128 );
	@Child public static final DisplayRenderObject logo			= make(  544,    0 );
	   
	//Deco   
	@Child public static final DisplayRenderObject gem			= make(   32,   96 );
	@Child public static final DisplayRenderObject lightball	= make(  128,   64 );
	@Child public static final DisplayRenderObject stone		= make(  480,    0 );
	@Child public static final DisplayRenderObject torch		= make(  320,   32 );
	@Child public static final DisplayRenderObject tree			= make(  448,    0 );
	@Child public static final DisplayRenderObject rollBall		= make(  160,   64 );
	  
	//Fog   
	@Child public static final DisplayRenderObject nebula		= make(    0,   32 );
	@Child public static final DisplayRenderObject purpleFog	= make(   32,   32, ScrollerDRO.class ); 
	   
	//Plattform   
	@Child public static final DisplayRenderObject catapult		= make(  416,    0 );
	@Child public static final DisplayRenderObject deathPl		= make(  128,    0, RotationDRO.class, 0.4f / 1000f );
	@Child public static final DisplayRenderObject solidWall	= make( 32*0,    0 );
	@Child public static final DisplayRenderObject solidGrass	= make(  224,    0 );
	@Child public static final DisplayRenderObject solidDirt	= make(  288,    0 );
	@Child public static final DisplayRenderObject solidPurp	= make(   64,   32 );
	@Child public static final DisplayRenderObject solidWhite	= make(    0,   96 );
	@Child public static final DisplayRenderObject spring		= make( 32*6,    0 );
	   
	//Zone   
	@Child public static final DisplayRenderObject elevator		= make(  320,    0 );
	@Child public static final DisplayRenderObject elevatorHozi = make(  352,   32 );
	@Child public static final DisplayRenderObject error		= make(  256,    0 );
	@Child public static final DisplayRenderObject help			= make(   32,    0 );
	@Child public static final DisplayRenderObject leave		= make(  224,   32 );
	@Child public static final DisplayRenderObject story		= make(  224,   64 );
	@Child public static final DisplayRenderObject negator		= make( 32*5,    0 );
	@Child public static final DisplayRenderObject normalizer	= make(  384,    0 );
	@Child public static final DisplayRenderObject portal0		= make( 0*32,   64 );
	@Child public static final DisplayRenderObject portal1		= make( 1*32,   64 );
	@Child public static final DisplayRenderObject portal2		= make( 2*32,   64 );
	@Child public static final DisplayRenderObject portal3		= make( 3*32,   64 );
	@Child public static final DisplayRenderObject resetOn		= make(   64,    0 );
	@Child public static final DisplayRenderObject resetOff		= make(   96,    0 );
	@Child public static final DisplayRenderObject sky			= make(  352,    0 );
	@Child public static final DisplayRenderObject skyPurple	= make(   96,   32 );
	@Child public static final DisplayRenderObject skyWhite		= make(  288,   32 );
	@Child public static final DisplayRenderObject spawner		= make(  160,   32 );
	@Child public static final DisplayRenderObject gravitron	= make(  288,   96 );
	
	//TD-Zone
	@Child public static final DisplayRenderObject floorGrass	= make(  608,    0 );
	@Child public static final DisplayRenderObject floorSand	= make(  640,    0 );
	
	@Child public static final DisplayRenderObject bBasePlanet  = make(  224,   96 );
	@Child public static final DisplayRenderObject bBaseIsland  = make(  256,   96 );
	
	//GUI
	@Child public static final DisplayRenderObject toleranceBar	= make(  224,  128, 64, 6 );
	@Child public static final DisplayRenderObject toleranceBarS= make(  224,  134, 7, 14 );
	@Child public static final DisplayRenderObject direction	= make(  192,  128 );
	@Child public static final DisplayRenderObject guWayMove	= make(   64,  128 );
	@Child public static final DisplayRenderObject guWayDest	= make(   96,  128 );
	@Child public static final DisplayRenderObject guWayErro	= make(  128,  128 );
	@Child public static final DisplayRenderObject crosshair	= make(  160,  128 );
	@Child public static final DisplayRenderObject lightDispOn	= make(  320,   96 );
	@Child public static final DisplayRenderObject lightDispOff	= make(  352,   96 );
	
	//Player
	@Child public static final DisplayRenderObject playerSWalk1	= make(    7,  160, 22, 32 );
	@Child public static final DisplayRenderObject playerSWalk2	= make(   71,  160, 22, 32 );
	@Child public static final DisplayRenderObject playerSStand	= make(   39,  160, 22, 32 );
	
	//GU
	@Child public static final DisplayRenderObject guMob =		   new DisplayRenderObject( 0xff00ff00 );
	@Child public static final DisplayRenderObject tdPlayerStand = make(   96,  160 );
	@Child public static final DisplayRenderObject tdPlayerWalk1 = make(  128,  160 );
	@Child public static final DisplayRenderObject tdPlayerWalk2 = make(  160,  160 );
	@Child public static final DisplayRenderObject ufoPlain		=  make(    0,  192, 32, 26 );
	@Child public static final DisplayRenderObject ufoThrottle	=  make(   32,  192, 32, 30 );
	
	
	private final Set<DisplayRenderObject> stepDROs = new HashSet<>();
	
	private static DisplayRenderObject make( int sx, int sy ) {
		return make( sx, sy, 32, 32, DisplayRenderObject.class );
	}
	
	private static DisplayRenderObject make( int sx, int sy, int sw, int sh ) {
		return make( sx, sy, sw, sh, DisplayRenderObject.class );
	}
	
	private static DisplayRenderObject make( int sx, int sy, Class<? extends DisplayRenderObject> type, Object... params  ) {
		return make( sx, sy, 32, 32, type, params );
	}
	
	private static DisplayRenderObject make( int sx, int sy, int sw, int sh, Class<? extends DisplayRenderObject> type, Object... params  ) {
		try {
			Object tr = new TextureRep( TextureRep.TextureValue.Tiles, sx, sy, sw, sh );
			DisplayRenderObject dro = (DisplayRenderObject)( type.getConstructor( TextureRep.class ).newInstance( tr ) );
			dro.set( params );
			return dro;
		} catch ( Exception ex ) {
			throw new OceanException( ex );
		}
	}

	@Override
	protected void init() {
		super.init();
		
		this.setIgnoreChildUpdate( true );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		for( DisplayRenderObject dro : stepDROs )
			dro.step( elapsedTime );
	}
	
	public void addForStep( DisplayRenderObject dro ) {
		stepDROs.add( dro );
	}
	
}
